var ns, toast,dialog;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
        },
        list: [],
        sum:0,
        selectlist:[],
        IsEditTotalCost:false,
        IsShowEditCost:false,
        selectform:{},
    },
    methods: {
        get_data: function() {
            var that = this;
            var newlist=that.list;
            that.list=that.selectlist; 
            if (that.selectlist.length > 0) {
                if (newlist.length > 0) {
                    for (var i = 0; i < that.list.length; i++) {
                        let result = newlist.find((item) => {
                            return item.ID === that.list[i].ID;
                        });
                        //之前选择的数据更改过数量
                        if (result) {
                            that.list[i].ItemNumber = result.ItemNumber;
                            that.list[i].ManNumber = result.ManNumber;
                            that.list[i].TotalPrice = result.TotalPrice;
                        }
                    }
                }
            }
            that.updateNum();
        },
        do_save: function() {
            var that = this;
            if (that.list.length<=0) {
                api.toast({
                    msg: '请选择费用',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            //判断
            for (var i = 0; i < that.list.length; i++) {
                if (Number(that.list[i].TotalPrice <= 0)) {
                    api.toast({
                        msg:'选择的"' + that.list[i].ItemName+ '"维修项目应收金额为0,请输入材料/人工数量',
                        duration: 3000,
                        location: 'bottom'
                    });
                    return;
                }
            }
            ns.post({
                action: 'dosaveservicecharge',
                ServiceID: that.form.ID,
                list: JSON.stringify(that.list)
            }, function(succeed, data, err) {
                if (succeed) {
                    if (!data.status && data.Msg) {
                        api.toast({
                            msg:  data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                    api.sendEvent({
                        name: 'dosaveservicefeedone',
                        extra: {
                            IDList: JSON.stringify(data.IDList),
                        }
                    });
                    //刷新详情界面
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_editcost:function(item){
            var that=this;
            that.IsShowEditCost=true;
            that.selectform=item;
            // dialog.prompt({
            //     title:"",
            //     text:'请输入新的收款金额',
            //     type: 'number',
            //     value: item.TotalPrice,
            //     buttons:['取消','确定']
            // },function(ret){
            //     if(ret.buttonIndex == 2){
            //         item.TotalPrice = ret.text == '' ? 0 : ret.text;
            //         item.TotalPrice=item.TotalPrice.toFixed(2);
            //         that.updateNum();
            //     }
            // })
        },
        do_editcost_cancel(){
            var that=this;
            that.IsShowEditCost=false;
        },
        do_editcost_ok(){
            var that=this;
            if (that.selectform.FactManPrice < 0) {
                that.selectform.FactManPrice = 0;
            }
            if (that.selectform.FactItemPrice < 0) {
                that.selectform.FactItemPrice = 0;
            }
            that.selectform.TotalPrice= Number(that.selectform.FactManPrice)+Number(that.selectform.FactItemPrice);
            that.selectform.TotalPrice=that.selectform.TotalPrice.toFixed(2);
            that.updateNum();
            that.do_editcost_cancel();
        },
        updateNum:function(){
            var that=this;
            var newSum=0;
            for (var i = 0; i < that.list.length; i++) {
                newSum+=Number(that.list[i].TotalPrice);
            }
            that.sum=newSum.toFixed(2);
        },
        doChangeNumber(item){
            var that=this;
            item.FactManPrice = item.ManUnitPrice * item.ManNumber;
            item.FactItemPrice = item.ItemUnitPrice * item.ItemNumber;
            item.TotalPrice = item.FactManPrice + item.FactItemPrice;
            that.updateNum();
        },
        open_shoufei:function(){
            var that = this;
            var selectIDs=[];
            if(that.list.length > 0){
                that.list.forEach(function (item) {
                    selectIDs.push(item.ID);
                })
            }
            ns.openWin('neibubaoshi_shoufei', '收费标准', {
                id: that.form.ID,
                selectIDs:selectIDs,
            });
        },
        doChargeRemove: function (ID) {
            var that = this;
            for (var i = 0; i < that.list.length; i++) {
                var item = that.list[i];
                if (item.ID == ID) {
                    that.list.splice(i, 1);
                }
            }
            that.updateNum();
        },
    },
});

apiready = function() {
    dialog = new auiDialog();
    ns = window.Foresight.Util;
    app.form.ID = api.pageParam.id || 0;
    app.IsEditTotalCost=api.pageParam.IsEditTotalCost || false;
   
    api.addEventListener({
        name: 'do_open_fee_history'
    }, function (ret, err) {
        app.open_shoufei();
    });

    api.addEventListener({
        name: 'do_loadprice_list'
    }, function (ret, err) {
        app.selectlist=ret.value.data;
        app.get_data();
    });
}
